
rm(list = ls())

## R functions
source("functions/pica2.R")
source("functions/pica1_use.R")
source("functions/bounds_frechet_function_for_pica2.R")
library(tidyverse)

# loading data
load("data/pica2_data.rdata")


## ################################
## Political Models as Outcomes
## ################################
out_political <- pica2(Y = "political_model", D = "D", A = "A", C = "actual_choice", 
                       covariates = c("female", "education", "national_pride", "ideology_right", "country"),
                       placebo = "placebo", placebo_level = 3, data = data)
rownames(out_political$ATE) <- c("US vs Nature", "China vs Nature")  
rownames(out_political$ACTE_view) <- c("Effect of US video on US-viewer", "Effect of China video on China-viewer")
rownames(out_political$ACTE_non_view) <- c("Effect of US video on non-US-viewer", "Effect of China video on non-China-viewer")

ACTE_political <- rbind(out_political$ACTE_view, out_political$ACTE_non_view)


#### Checking Valid-Placebo Assumption
test_political <- test_pica2(Y = "political_model", D = "D", A = "A", C = "actual_choice", 
                             placebo = "placebo", placebo_level = 3, data = data)
test_political

#### Analyzing Political Model as PICA-1
out_political_pica1 <- pica1_after_pica2(Y = "political_model", D = "D", A = "A", 
                                         C = "actual_choice", S = "stated_preference_num",
                                         placebo = "placebo", placebo_level = 3, 
                                         data = data, seed = 1234)

out_political_pica1_tab<- rbind(out_political_pica1[out_political_pica1$treat == 1 & out_political_pica1$ctrl == 3 & out_political_pica1$C == 1, ],
                                out_political_pica1[out_political_pica1$treat == 2 & out_political_pica1$ctrl == 3 & out_political_pica1$C == 2, ],
                                out_political_pica1[out_political_pica1$treat == 1 & out_political_pica1$ctrl == 3 & out_political_pica1$C == -1, ],
                                out_political_pica1[out_political_pica1$treat == 2 & out_political_pica1$ctrl == 3 & out_political_pica1$C == -2, ])
rownames(out_political_pica1_tab) <- c("Effect of US video on US-viewer", "Effect of China video on China-viewer", 
                                       "Effect of US video on non-US-viewer", "Effect of China video on non-China-viewer")

#### Storing the results
Effect_US    <- rbind(out_political$ATE[1, ], ACTE_political[c(1,3), ])
Effect_China <- rbind(out_political$ATE[2, ], ACTE_political[c(2,4), ])
Effect_US_bound    <- out_political_pica1_tab[c(1,3), -c(1,2,3)]
Effect_China_bound <- out_political_pica1_tab[c(2,4), -c(1,2,3)]

political_list <- list("Effect_US" = Effect_US, "Effect_China" = Effect_China, 
                       "Effect_US_bound" = Effect_US_bound, "Effect_China_bound" = Effect_China_bound)
save(political_list, file = "pica2_political_final.rdata")



## ################################
## Economic Models as Outcomes
## ################################
out_econ <- pica2(Y = "econ_model", D = "D", A = "A", C = "actual_choice", 
                  covariates = c("female", "education", "national_pride", "ideology_right", "country"),
                  placebo = "placebo", placebo_level = 3, data = data)
rownames(out_econ$ATE) <- c("US vs Nature", "China vs Nature")  
rownames(out_econ$ACTE_view) <- c("Effect of US video on US-viewer", "Effect of China video on China-viewer")
rownames(out_econ$ACTE_non_view) <- c("Effect of US video on non-US-viewer", "Effect of China video on non-China-viewer")
ACTE_econ <- rbind(out_econ$ACTE_view, out_econ$ACTE_non_view)

#### Checking Valid-Placebo Assumption
test_econ <- test_pica2(Y = "econ_model", D = "D", A = "A", C = "actual_choice", 
                        placebo = "placebo", placebo_level = 3, data = data)
test_econ

#### Analyzing Econ Model as PICA-1
out_econ_pica1 <- pica1_after_pica2(Y = "econ_model", D = "D", A = "A", C = "actual_choice", S = "stated_preference_num",
                                    placebo = "placebo", placebo_level = 3, data = data, seed = 1234)
out_econ_pica1_tab<- rbind(out_econ_pica1[out_econ_pica1$treat == 1 & out_econ_pica1$ctrl == 3 & out_econ_pica1$C == 1, ],
                           out_econ_pica1[out_econ_pica1$treat == 2 & out_econ_pica1$ctrl == 3 & out_econ_pica1$C == 2, ],
                           out_econ_pica1[out_econ_pica1$treat == 1 & out_econ_pica1$ctrl == 3 & out_econ_pica1$C == -1, ],
                           out_econ_pica1[out_econ_pica1$treat == 2 & out_econ_pica1$ctrl == 3 & out_econ_pica1$C == -2, ])
rownames(out_econ_pica1_tab) <- c("Effect of US video on US-viewer", "Effect of China video on China-viewer", 
                                  "Effect of US video on non-US-viewer", "Effect of China video on non-China-viewer")

#### Storing the results
Effect_US    <- rbind(out_econ$ATE[1, ], ACTE_econ[c(1,3), ])
Effect_China <- rbind(out_econ$ATE[2, ], ACTE_econ[c(2,4), ])
Effect_US_bound    <- out_econ_pica1_tab[c(1,3), -c(1,2,3)]
Effect_China_bound <- out_econ_pica1_tab[c(2,4), -c(1,2,3)]

econ_list <- list("Effect_US" = Effect_US, "Effect_China" = Effect_China, 
                  "Effect_US_bound" = Effect_US_bound, "Effect_China_bound" = Effect_China_bound)
save(econ_list, file = "pica2_econ_final.rdata")